<template>
  <el-container style="height: 100%; min-width: 600px">
    <el-header>
      <el-row>
        <el-button type="success"
                   icon="el-icon-plus"
                   @click="centerDialogVisible = true"
                   circle></el-button>
      </el-row>

      <!-- 弹出添加对话框 -->
      <el-dialog title="新增车辆"
                 :visible.sync="centerDialogVisible"
                 width="30%"
                 center>
        <!-- 内容 -->
        <el-form label-position='left'
                 label-width="80px"
                 :model="formLabelAlign">
          <el-form-item label="拥有者">
            <el-input v-model="formLabelAlign.user_id"></el-input>
          </el-form-item>
          <el-form-item label="车型">
            <el-radio-group v-model="formLabelAlign.car_type">
              <el-radio-button label="bike">自行车</el-radio-button>
              <el-radio-button label="moto">摩托车</el-radio-button>
              <el-radio-button label="car">汽车</el-radio-button>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="租金">
            <el-input v-model="formLabelAlign.car_gold"></el-input>
          </el-form-item>
          <el-form-item label="地址">
            <el-input v-model="formLabelAlign.car_address"></el-input>
          </el-form-item>
          <!-- 上传文件 -->
          <el-upload class="upload-demo"
                     ref="upload"
                     action="http://121.37.209.61:8888/car/register"
                     :data="formLabelAlign"
                     :limit=1
                     :on-preview="handlePreview"
                     :on-remove="handleRemove"
                     :on-success="response => uploadOK(response)"
                     :file-list="fileList"
                     :auto-upload="false">
            <el-button slot="trigger"
                       size="small"
                       type="primary">选取文件</el-button>
            <div slot="tip"
                 class="el-upload__tip">最多只能上传一个文件</div>
          </el-upload>
        </el-form>
        <!-- 内容 -->
        <span slot="footer"
              class="dialog-footer">
          <el-button @click="centerDialogVisible = false">取 消</el-button>
          <el-button type="primary"
                     @click="addCar">确 定</el-button>
        </span>
      </el-dialog>
   
    </el-header>
    <el-main>
      <!-- 表格 -->
      <el-table v-loading="tableLoading"
                type="index"
                align="center"
                :data="tableData"
                stripe
                style="width: 100%"
                height="100%">
        <el-table-column fixed
                         prop="car_id"
                         label="车辆ID"
                         width=""
                         align="center">
        </el-table-column>
        <el-table-column prop="user_id"
                         label="拥有者ID"
                         width=""
                         align="center">
        </el-table-column>
        <el-table-column prop="person_id"
                         label="借用者ID"
                         width=""
                         align="center">
        </el-table-column>
        <el-table-column prop="car_type"
                         label="车辆类型"
                         width=""
                         align="center">
        </el-table-column>
        <el-table-column prop="car_gold"
                         label="租金"
                         width=""
                         align="center">
        </el-table-column>
        <el-table-column prop="car_address"
                         label="车辆地址"
                         width=""
                         align="center">
        </el-table-column>
        <el-table-column prop="car_img"
                         label="车辆图片"
                         width=""
                         align="center">
          <template slot-scope="scope">
            <img :src="tableData[scope.$index].car_img"
                 alt="" />
          </template>
        </el-table-column>
        <el-table-column fixed="right"
                         label="操作"
                         width="200"
                         align="center">
          <template slot-scope="scope">
            <el-switch v-model="tableData[scope.$index].car_statu"
                       active-text=""
                       inactive-text=""
                       @change="switchfun(scope.$index, scope.row)">
            </el-switch>
            <el-button size="mini"
                       @click="handleEdit(scope.$index, scope.row)">
              编辑
            </el-button>
            <el-button size="mini"
                       type="danger"
                       @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

    </el-main>
  </el-container>
</template>

<script>
import { HttpGet, HttpPost } from '@/api/http'

export default {
  name: "Car",
  data () {
    return {
      centerDialogVisible: false,
      fileList: [],
      formLabelAlign: {
        user_id: '',
        car_type: '',
        car_gold: '',
        car_address: ''
      },
      tableData: [],
      tableLoading: true
    };
  },
  created () {
    this.getList()
  },
  methods: {
    //添加车辆
    addCar () {
      this.centerDialogVisible = false
      this.submitUpload()
    },
    //上传文件
    submitUpload () {
      this.$refs.upload.submit();
    },
    //上传成功回调函数
    uploadOK (res) {
      console.log(res);
      if (res.code == 200) {
        //刷新列表
        this.getList()
        //表单置空
        this.formLabelAlign.user_id = '',
          this.formLabelAlign.car_type = '',
          this.formLabelAlign.car_gold = '',
          this.formLabelAlign.car_address = ''
        this.fileList = []
        this.$message({
          message: '成功,上传记录成功！',
          type: 'success'
        });
      } else {
        this.$message.error('错误，上传记录失败！');
      }
    },
    handleRemove (file, fileList) {
      console.log(file, fileList);
    },
    handlePreview (file) {
      console.log(file);
    },
    //开关事件
    async switchfun (index, row) {
      let res = await HttpPost({
        url: '/car/updateCarStatu',
        data: {
          car_id: row.car_id,
          car_statu: row.car_statu ? '1' : '0'
        }
      })
      if (res.data.code == 200) {
        this.$message.success('成功,切换车辆状态成功！')
      } else {
        this.$message.error('错误,切换车辆状态失败！');
      }
    },
    // 删除行
    async handleDelete (index, rows) {
      let res = await HttpGet({
        url: "/car/delete",
        data: {
          car_id: rows.car_id
        }
      })
      if (res.data.code == 200) {
        // 假删除
        this.tableData.splice(index, 1)
        this.$message({
          message: '成功，删除车辆成功',
          type: 'success'
        });
      } else {
        this.$message.error('失败，删除车辆失败');
      }
    },
    // 点击单个编辑
    handleEdit (index, row) {
      console.log(index, row);
    },
    //请求列表
    async getList () {
      this.tableLoading = true
      try {
        let res = await HttpGet({ url: '/car/all' })
        if (res.data.code == 200) {
          this.tableLoading = false
          this.tableData = res.data.data.map(item => {
            item.car_statu = (item.car_statu == '1' ? true : false)
            return item
          })
        }
      } finally {
        this.tableLoading = false
      }
    }
  },
};
</script>

<style scoped>
.el-header {
  padding-top: 20px;
  display: flex;
  justify-content: space-between;
}

.el-main {
  min-width: 600px;
  height: 100%;
  overflow: hidden;
}

/* 车辆列表图片 */
.el-table img {
  width: 80px;
  height: 80px;
}
</style>
